package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"shuqi-novel/mysql"

	"shuqi-novel/userrpc/internal/svc"
	"shuqi-novel/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ArticleUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewArticleUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ArticleUpdateLogic {
	return &ArticleUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 小说更新
func (l *ArticleUpdateLogic) ArticleUpdate(in *userrpc.ArticleUpdateRequest) (*userrpc.ArticleUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var article mysql.Article
	err := article.FindArticle(mysql.DB, in.ArticleId)
	if err != nil {
		return nil, errors.New("文章不存在")
	}
	article = mysql.Article{
		Model:   gorm.Model{ID: uint(in.ArticleId)},
		UserId:  in.UserId,
		Title:   in.Title,
		About:   in.About,
		Type:    in.Type,
		Content: in.Content,
	}

	err = article.ArticleUpdate(mysql.DB)
	if err != nil {
		return nil, errors.New("修改失败")
	}

	return &userrpc.ArticleUpdateResponse{
		ArticleId: in.ArticleId,
	}, nil
}
